import java.util.Arrays;
import java.util.Random;

public class Main {
    static Random rn = new Random(System.currentTimeMillis());
    public static int getRandomInt(int i){
        return Math.abs(rn.nextInt())%i + 1;
    }
//    public static int getRandomIntFrom2(int i){
//        return Math.abs(rn.nextInt())%i + 2;
//    }

    int[] genNums(){
        int len = getRandomInt(10000);
        int[] array = new int[len];
        for(int i=0; i<len; i++){
            array[i] = getRandomInt(10000);
        }

        return array;
    }

    public void test(){
        int nums[] = genNums();
        int s = getRandomInt(10000000);
        String input = arrayToString(nums);

        Solution solution = new Solution();

        int expected = minSubArrayLen(s, nums);
        int actual = solution.minSubArrayLen(s, nums);
        if(expected != actual){
            String msg = "Test Failed\n";
            msg += "Input:" + input + "\n";
            msg += "Expected:" + expected + "\n";
            msg += "Actual:" + actual;
            throw new RuntimeException(msg);
        }
    }

    public void customizedTest01() {
        int[] nums = {2,3,1,2,4,3};
        int s = 7;
        testCustomized(s, nums);
    }

    public void customizedTest02() {
        int[] nums = new int[100000];
        Arrays.fill(nums, 1);
        nums[99999] = 4;
        nums[99998] = 3;
        int s = 7;
        testCustomized(s, nums);
    }

    public void testCustomized(int s, int[] nums){


//        String input = arrayToString(nums);

        Solution solution = new Solution();

        int expected = minSubArrayLen(s, nums);
        int actual = solution.minSubArrayLen(s, nums);

//        System.out.println("expected: " + expected);
//        System.out.println("actual: " + actual);

        if(expected != actual){
            String msg = "Test Failed\n";
            msg += "Input:"  + "\n";
            msg += "Expected:" + expected + "\n";
            msg += "Actual:" + actual;
            throw new RuntimeException(msg);
        }
    }

    private String arrayToString(int[] num) {
        String ret = "[";
        for(int i=0; i<num.length; i++){
            if(i != num.length -1){
                ret += num[i] + ",";
            }else{
                ret += num[i];
            }
        }
        return ret + "]";
    }

    public static void main(String[] args) {
        try {
            Main main = new Main();
//            for (int i = 0; i < 20; i++) {
//                main.test();
//            }
            main.customizedTest01();
            main.customizedTest02();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
            System.exit(-1);
        }
    }
    public int minSubArrayLen(int s, int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int ans = Integer.MAX_VALUE;
        int[] sums = new int[n + 1];
        // 为了方便计算，令 size = n + 1
        // sums[0] = 0 意味着前 0 个元素的前缀和为 0
        // sums[1] = A[0] 前 1 个元素的前缀和为 A[0]
        // 以此类推
        for (int i = 1; i <= n; i++) {
            sums[i] = sums[i - 1] + nums[i - 1];
        }
        for (int i = 1; i <= n; i++) {
            int target = s + sums[i - 1];
            int bound = Arrays.binarySearch(sums, target);
            if (bound < 0) {
                bound = -bound - 1;
            }
            if (bound <= n) {
                ans = Math.min(ans, bound - (i - 1));
            }
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
